<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src\system\Director.js - Go2d</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="icon" href="../assets/favicon.ico">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
                <h1><img src="../assets/css/logo.png" title="Go2d" width="117" height="52"></h1>
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: 1.6.7</em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
                    <h2 class="off-left">APIs</h2>
                    <div id="api-tabview" class="tabview">
                        <ul class="tabs">
                            <li><a href="#api-classes">Classes</a></li>
                            <li><a href="#api-modules">Modules</a></li>
                        </ul>
                
                        <div id="api-tabview-filter">
                            <input type="search" id="api-filter" placeholder="Type to filter APIs">
                        </div>
                
                        <div id="api-tabview-panel">
                            <ul id="api-classes" class="apis classes">
                                <li><a href="../classes/Class.html">Class</a></li>
                                <li><a href="../classes/Director.html">Director</a></li>
                                <li><a href="../classes/DisplayObject.html">DisplayObject</a></li>
                                <li><a href="../classes/Ease.html">Ease</a></li>
                                <li><a href="../classes/Event.html">Event</a></li>
                                <li><a href="../classes/EventEmitter.html">EventEmitter</a></li>
                                <li><a href="../classes/go2d.html">go2d</a></li>
                                <li><a href="../classes/ImageView.html">ImageView</a></li>
                                <li><a href="../classes/Matrix.html">Matrix</a></li>
                                <li><a href="../classes/ObjectPool.html">ObjectPool</a></li>
                                <li><a href="../classes/ResizeEvent.html">ResizeEvent</a></li>
                                <li><a href="../classes/ResourceLoader.html">ResourceLoader</a></li>
                                <li><a href="../classes/ScrollEvent.html">ScrollEvent</a></li>
                                <li><a href="../classes/ScrollView.html">ScrollView</a></li>
                                <li><a href="../classes/Sprite.html">Sprite</a></li>
                                <li><a href="../classes/Stage.html">Stage</a></li>
                                <li><a href="../classes/TextField.html">TextField</a></li>
                                <li><a href="../classes/TouchEvent.html">TouchEvent</a></li>
                                <li><a href="../classes/Tween.html">Tween</a></li>
                                <li><a href="../classes/URLRequest.html">URLRequest</a></li>
                                <li><a href="../classes/Vector.html">Vector</a></li>
                            </ul>
                
                            <ul id="api-modules" class="apis modules">
                                <li><a href="../modules/go2d.html">go2d</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
                    Show:
                    <label for="api-show-inherited">
                        <input type="checkbox" id="api-show-inherited" checked>
                        Inherited
                    </label>
            
                    <label for="api-show-protected">
                        <input type="checkbox" id="api-show-protected">
                        Protected
                    </label>
            
                    <label for="api-show-private">
                        <input type="checkbox" id="api-show-private">
                        Private
                    </label>
                    <label for="api-show-deprecated">
                        <input type="checkbox" id="api-show-deprecated">
                        Deprecated
                    </label>
            
                </div>
            
            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
<h1 class="file-heading">File: src\system\Director.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
/**
 * 导演类，负责游戏逻辑流程的管理。
 * @author Lanfei
 * @class Director
 * @extends EventEmitter
 */
var Director = go2d.Director = EventEmitter.extend({
	__init: function() {
		this._super();

		/**
		 * 当前帧频
		 * @readonly
		 * @property fps
		 * @type number
		 */
		this.fps = 0;

		/**
		 * 舞台对象
		 * @protected
		 * @property _stage
		 * @type Stage
		 */
		this._stage = stage;

		/**
		 * 上一帧时间戳
		 * @protected
		 * @property _lastTime
		 * @type number
		 */
		this._lastTime = null;

		/**
		 * 是否已暂停
		 * @protected
		 * @property _paused
		 * @type Boolean
		 * @default true
		 */
		this._paused = true;

		/**
		 * 主循环定时器
		 * @protected
		 * @property _timer
		 * @type Object
		 */
		this._timer = null;

		this._initTimer();
		this._initEvent();
		this.run();
		Director.instance = this;
	},
	_initEvent: function() {
		var that = this;
		var sleep = false;
		var prefixes = [&#x27;&#x27;, &#x27;ms&#x27;, &#x27;moz&#x27;, &#x27;webkit&#x27;];
		forEach(prefixes, function(prefix) {
			if (document[prefix + &#x27;hidden&#x27;] !== undefined) {
				document.addEventListener(prefix + &#x27;visibilitychange&#x27;, function() {
					if (document[prefix + &#x27;hidden&#x27;]) {
						if (!that._paused) {
							that.pause();
							sleep = true;
						}
					} else if (sleep) {
						sleep = false;
						that.run();
					}
				});
				return false;
			}
		});
	},
	_initTimer: function() {

		function setTimeBasedTimer(callback) {
			return setTimeout(callback, 1000 / 60);
		}

		function clearTimeBasedTimer(timer) {
			return clearTimeout(timer);
		}

		window.requestAnimationFrame =
			window.requestAnimationFrame ||
			window.webkitRequestAnimationFrame ||
			window.mozRequestAnimationFrame ||
			setTimeBasedTimer;
		window.cancelAnimationFrame =
			window.cancelAnimationFrame ||
			window.webkitCancelAnimationFrame ||
			window.webkitCancelRequestAnimationFrame ||
			window.mozCancelAnimationFrame ||
			window.mozCancelRequestAnimationFrame ||
			clearTimeBasedTimer;
	},
	/**
	 * 游戏心跳
	 * @protected
	 * @function _tick
	 */
	_tick: function() {
		var deltaTime,
			now = +new Date();
		if (this._lastTime &gt; 0) {
			deltaTime = now - this._lastTime;
			this.fps = Math.round(1000 / deltaTime);
		} else {
			deltaTime = 1000 / 60;
			this.fps = 60;
		}
		this._lastTime = now;
		this.emit(&#x27;tick&#x27;, deltaTime);
	},
	/**
	 * 开始心跳
	 * @function run
	 * @return {this}
	 */
	run: function() {
		var that = this;
		this._timer = requestAnimationFrame(function() {
			that._tick();
			that.run();
		});
		this._paused = false;
		return this;
	},
	/**
	 * 暂停心跳
	 * @function pause
	 * @return {this}
	 */
	pause: function() {
		cancelAnimationFrame(this._timer);
		this._paused = true;
		this._lastTime = 0;
		return this;
	}
}, {
	/**
	 * 暂停心跳
	 * @static
	 * @protected
	 * @property _instance
	 * @type Director
	 */
	_instance: null,
	/**
	 * 暂停心跳
	 * @static
	 * @function getInstance
	 * @return {Director}
	 */
	getInstance: function() {
		if (!Director._instance) {
			Director._instance = new Director();
		}
		return Director._instance;
	}
});
    </pre>
</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>
